package com.example.demo.demos.book.service;

import com.example.demo.demos.book.enums.BookStatusEnum;
import com.example.demo.model.PageResult;
import com.example.demo.demos.book.mapper.BookInfoMapper;
import com.example.demo.model.BookInfo;
import com.example.demo.demos.book.dao.BookDao;
import com.example.demo.model.PageRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.print.Book;
import java.util.List;
@Slf4j
@Service
public class BookService {
    @Autowired
     private BookDao bookDao;
    @Autowired
    private BookInfoMapper bookInfoMapper;
//    private BookService bookService;
//
//    public BookController(){
//        bookService=new BookService();
//    }
//    public List<BookInfo> getBookList() {
//
//        List<BookInfo> bookInfos = bookDao.mockData();
//        for (BookInfo bookInfo : bookInfos) {
//            if (bookInfo.getStatus() == 1) {
//                bookInfo.setStatusCN("可借阅");
//            } else {
//                bookInfo.setStatusCN("不可借阅");
//            }
//        }
//        return bookInfos;
//    }

    public  PageResult<BookInfo> selectBookInfoByPage(PageRequest pageRequest){
        if(pageRequest==null){
             return  null;
        }
        Integer count=bookInfoMapper.count();
        //获取当前记录
        List<BookInfo> bookInfos=bookInfoMapper.selectBookInfoByPage(pageRequest.getOffset(), pageRequest.getPageSize());
        if(bookInfos!=null&&bookInfos.size()>0){
            for (BookInfo bookInfo:bookInfos){
//                if (bookInfo.getStatus()==1){
//                    bookInfo.setStatusCN("可借阅");
//                }else if(bookInfo.getStatus()==2){
//                    bookInfo.setStatusCN("不可借阅");
//                }
                //根据status，获取状态的定义
                bookInfo.setStatusCN(BookStatusEnum.getNameByCode(bookInfo.getStatus()).getName());
            }
        }
        return new PageResult<>(bookInfos,count,pageRequest);
    }

    public Integer addBook(BookInfo bookInfo){
        Integer result=null;
        try{
            result=bookInfoMapper.insertBook(bookInfo);
        }catch (Exception e){
            log.info("添加图书出错,e:{}",e);
        }
        return result;
    }
    public BookInfo queryBookInfoById(Integer id){
        return  bookInfoMapper.queryBookInfoById(id);
    }
    public Integer updateBook(BookInfo bookInfo){
        Integer result=0;
        try{
            result=bookInfoMapper.updateBook(bookInfo);
        }catch (Exception e){
            log.error("更新图书失败,e{}",e);
        }
        return result;
    }
    public Integer batchDelete(List<Integer> ids){
        Integer result =0;
        try {
            result = bookInfoMapper.batchDelete(ids);
        }catch (Exception e){
            log.error("批量删除图书失败, ids:{}",ids);
        }
        return result;
    }
}